package meatslope2.gameengine.input;

import javax.microedition.khronos.opengles.GL10;

import meatslope2.constants.BitmapConstants;
import meatslope2.constants.SpriteConstants;
import meatslope2.gameengine.gameobjects.model.DrawableGameObject;
import meatslope2.gameengine.helpers.Utils;
import meatslope2.gameengine.primatives.Vector2;

public class MovementButton extends CircleButton implements IMovementButton
{
	// ===========================================================
	// Constants
	// ===========================================================
	
	public final static int LEFT_ARROW = 0;
	public final static int RIGHT_ARROW = 1;
	
	//Jumping state
	private final static int WAITING_JUMP = 0;
	private final static int JUMPED = 1;
	
	// ===========================================================
	// Members
	// ===========================================================
	
	private int jumpingState = WAITING_JUMP;
	
	//Used to show the player how high he must lift the left or right thumbstick in order to jump
	private DrawableGameObject jumpBar;
	
	private int originalY; //Our reference to where this button started
	private int jumpThreshHoldY; //Our reference to where the user must drag the movement stick to jump
	
	private boolean touched = false; //So we can track whether a button was touched or not while we iterate through our touches
	
	// ===========================================================
	// Constructors
	// ===========================================================
	
	public MovementButton(int arrowType, int posX, int posY)
	{
		super(arrowType == LEFT_ARROW ? SpriteConstants.ARROW_LEFT : SpriteConstants.ARROW_RIGHT,
				BitmapConstants.MOVEMENT_CIRCLE_DIAMETER,
				posX,
				posY);
		
		this.originalY = posY;
		this.jumpThreshHoldY = posY + BitmapConstants.JUMP_DRAW_BAR_ACTUAL_HEIGHT - this.radius;
				
		this.jumpBar = new DrawableGameObject(SpriteConstants.JUMP_BAR, BitmapConstants.JUMP_DRAW_BAR_DRAWING_WIDTH, BitmapConstants.JUMP_DRAW_BAR_DRAWING_HEIGHT, false);
		final float jumpBarX = posX + (width / 2) - (BitmapConstants.JUMP_DRAW_BAR_DRAWING_WIDTH / 2);
		final float jumpBarY = posY;
		
		this.jumpBar.getPosition().set(jumpBarX, jumpBarY);
		this.jumpBar.setDraw(false);
	}
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	@Override
	public boolean pressed(Vector2 touchPosition) 
	{
		final boolean hit =  super.pressed(touchPosition);
		
		if(hit)
		{
			updateButton(touchPosition);
			this.touched = true;
		}
		
		return hit;
	}
	
	/**
	 * Everything that needs to be done when a movement button is touched
	 * 
	 * 1. Set the jumping bar to be drawn
	 * 2. Update position
	 */
	private void updateButton(Vector2 touchPosition) 
	{
		switch(jumpingState)
		{
			case WAITING_JUMP:
				if(touchPosition.y >= jumpThreshHoldY)
				{
					performJump();
					jumpingState = JUMPED;
				}
				break;
			case JUMPED:
				if(touchPosition.y < jumpThreshHoldY)
					jumpingState = WAITING_JUMP;
				break;
		}
			
		this.position.y = Utils.clamp((int)touchPosition.y - this.radius, this.originalY, this.jumpThreshHoldY);
		this.jumpBar.setDraw(true);
	}

	public void reset()
	{
		this.jumpBar.setDraw(false);
		this.position.y = this.originalY;
	}
	
	@Override
	public void draw(GL10 pGL)
	{
		this.jumpBar.draw(pGL);
		super.draw(pGL);
	}

	public void performJump() 
	{
		//unimplemented by default
	}
	
	public void startTouches()
	{
		this.touched = false;
	}
	
	public void endTouches()
	{
		if(!this.touched)
			reset();
	}
}
